﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace RPGClient
{
    public class RPGNETClient
    {
        public Socket Socket;
        private ReceiveThread _recvThread;

        public int Connect()
        {
            _recvThread = new ReceiveThread(this);

            Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress address = IPAddress.Parse("172.17.125.138");
            IPEndPoint endPoint = new IPEndPoint(address, 8599);
            try
            {
                Socket.Connect(endPoint);
                _recvThread.Start();
                return 1;
            }
            catch
            {
                return 2;
            }
        }

        public int DisConnect()
        {
            _recvThread.Stop();
            Socket.Disconnect(false);
            return 0;
        }

        public int Send(byte[] buffer, int length)
        {
            //_clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnSendCallback), null);
            try
            {
                //_clientSocket.Send(buffer,);
                Socket.BeginSend(buffer, 0, length, SocketFlags.None, new AsyncCallback(OnSendCallback), null);
                return 1;
            }
            catch
            {
                return 2;
            }
        }


        void OnSendCallback(IAsyncResult ar)
        {
            Socket.EndSend(ar);
        }
    }
}
